<template>
  <div class="login-box">
    <div class="title">
      <div class="left">
        登录
      </div>
      <!--<div class="middle">-->
      <!--  /-->
      <!--</div>-->
      <!--<div class="right" @click="toRegister">-->
      <!--  注册-->
      <!--</div>-->
    </div>
    <div class="login-body">
      <el-form
          ref="loginRuleFormRef"
          :model="loginForm"
          :rules="LoginRules"
          label-width="120px"
          class="login-form"
          status-icon
          @keyup.enter.native="showYanzhengma(loginRuleFormRef)"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="loginForm.name" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password"  type="password" />
        </el-form-item>
        <el-form-item class="submit">
          <el-button
              type="primary"
              @click="showYanzhengma(loginRuleFormRef)"
              class="login-form-btn"
              size="large"
          >
            登录
          </el-button>
          <el-button
              @click="resetForm(loginRuleFormRef)"
              class="login-form-btn"
              size="large"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

  <!--验证码-->
  <Vcode :show="isShow" @success="onSuccess" @close="onClose" @fail="fail"/>
</template>

<script setup>
import {ElForm, ElFormItem, ElInput, ElButton, ElMessage} from "element-plus";
import {reactive, ref, } from "vue";
import {useRouter} from "vue-router";
import {login} from "@/api/index.js"
import Vcode from "vue3-puzzle-vcode";

const router = useRouter()
const loginRuleFormRef = ref()
// 登录表单
let loginForm = reactive({
  name:'',
  password:'',
})
// 验证规则
const LoginRules = reactive(
    {
      name: [
        {required: true, message: '用户名不能为空', trigger: 'blur'},
        {min: 2, max: 10, message: '长度必须为2-10个字符', trigger: 'blur'},
      ],
      password: [
        {required: true, message: '密码不能为空', trigger: 'blur'},
        {min: 6, max: 15, message: '长度必须为6-15个字符', trigger: 'blur'},
      ],
      ver: [
        {required: true, message: '验证码不能为空', trigger: 'blur'},
      ]
    }
)
// 验证码显示
const isShow = ref(false);
const toRegister = ()=>{
  window.localStorage.removeItem('syhyDocManSys')
  router.push('register')
}
//
const showYanzhengma = (formEl)=>{
  if (!formEl) return
  isShow.value = true
}

//用户登录

const resetForm = (f)=>{
  if (!f) return
  f.resetFields()
}

// 验证码成功的回调
const onSuccess = async () => {
  try {
    onClose(); // 验证成功，手动关闭模态框
    // console.log(loginForm)
    const {data:res} =await login({
      username:loginForm.name,
      password:loginForm.password,
    })
    if(res.code === 200){
      ElMessage({
        message:'登录成功!',
        type:'success'
      })
      //清空表单
      loginForm = {
        name:'',
        password:'',
      }
      // console.log(res)
      const userInfo = {
        id:res.data.id,
        token:res.data.token,
        username:res.data.username,
        root:res.data.root,
        role:res.data.role
      }
      // console.log(userInfo)
      localStorage.setItem('syhyDocManSys',JSON.stringify(userInfo))
      await router.push('/homePage')
    } else if(res.code === 400){
      return ElMessage({
        message:'用户名或密码错误',
        type:'warning'
      })
    } else if (res.code === 401){
      return ElMessage({
        message:'请正确填写用户名和密码',
        type:'warning'
      })
    } else{
      return ElMessage({
        message:'登录失败',
        type:'warning'
      })
    }
  } catch (e) {
    console.log(e)
  }
};

const fail = ()=>{
  // alert('验证失败!')
}

// 关闭验证码
const onClose = () => {
  isShow.value = false;
};
</script>

<style scoped lang="less">
.login-box{
  background-color: rgba(255,255,255,.4);
  position: absolute;
  left: 65%;
  top: 26%;
  border: solid 2px white;
  height: 45%;
  width: 25%;
  box-shadow: 10px 10px 25px #87CEFA;
  .title{
    height: 10%;
    display: flex;
    color: white;
    font-size: 3vh;
    text-align: left;
    margin-left: 2vw;
    margin-top: 3vh;
    align-items: baseline;
    letter-spacing: 6px;
    .left{
      margin-right: 0.5vw;
      text-shadow: 0px 0px 2vw #1874CD;
    }
    .right{
      font-size: 1.8vh;
      cursor: pointer;
      margin-left: 0.5vw;
      text-shadow: 0px 0px 2vw #1874CD;
      &:hover{
        text-decoration: underline;
        color: #0066ff;
      }
    }
  }
  .login-body{
    width: 90%;
    height: 55%;
    margin: 6vh auto;
    /deep/.el-form-item {
      display: flex;
      --font-size: 14px;
      margin-bottom: 3vh;
    }
    /deep/.el-form-item__label {
      justify-content: center;
      align-items: flex-start;
      font-size: 1.8vh !important;
      letter-spacing: 1px;
    }
    /deep/.el-input{
      width: 80%;
    }
    .submit{
      margin-top: 15%;
      justify-content: center;
      /deep/.el-form-item__content {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        flex: 1;
        line-height: 32px;
        position: relative;
        font-size: var(--font-size);
        min-width: 0;
        margin-left: 0 !important;
      }
    }
    .login-form{}
  }
}
</style>